package controller

import (
	"dc-news/common/global/consts"
	"dc-news/common/global/setting"
	myjwt "dc-news/common/middleware/jwt"
	"dc-news/common/resp"
	"dc-news/service"
	"dc-news/vo"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"net/http"
)

// GetNewsListByChannelHandler 获取新闻列表
func GetNewsListByChannelHandler(c *gin.Context) {
	channel := c.Query("channel")
	count := c.Query("count")
	if channel == "" {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	if _, ok := setting.Channels[channel]; !ok {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	if count == "0" {
		count = "10"
	}
	if channel == "__all__" {
		service.GetNewsListByRecommendService(c, cast.ToInt(count))
	} else {
		service.GetNewsListByChannelService(c, channel, cast.ToInt(count))
	}
}

// GetNewsHandler 获取新闻
func GetNewsHandler(c *gin.Context) {
	idStr, ok := c.Params.Get("id")
	id := cast.ToInt(idStr)
	if !ok || id == 0 {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	service.GetNewsService(c, id)
}

// AddFavouriteNewsHandler 添加收藏
func AddFavouriteNewsHandler(c *gin.Context) {
	idStr, ok := c.Params.Get("id")
	id := cast.ToInt(idStr)
	if !ok || id == 0 {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	service.AddFavouriteNewsService(c, cast.ToInt(claims.Id), id)
}

// RemoveFavouriteNewsHandler 取消收藏
func RemoveFavouriteNewsHandler(c *gin.Context) {
	idStr, ok := c.Params.Get("id")
	id := cast.ToInt(idStr)
	if !ok || id == 0 {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	service.RemoveFavouriteNewsService(c, cast.ToInt(claims.Id), id)
}

// AddChannelHandler 关注频道
func AddChannelHandler(c *gin.Context) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	req := &vo.AddChannelReq{}
	err := c.Bind(req)
	if err != nil {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	ok := setting.Channels[req.Channel]
	if !ok {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "频道不存在"))
		return
	}
	service.AddChannelService(c, cast.ToInt(claims.Id), req)
}

// RemoveChannelHandler 取消关注频道
func RemoveChannelHandler(c *gin.Context) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	req := &vo.RemoveChannelReq{}
	err := c.Bind(req)
	if err != nil {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	ok := setting.Channels[req.Channel]
	if !ok {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "频道不存在"))
		return
	}
	service.RemoveChannelService(c, cast.ToInt(claims.Id), req)
}

// LikesNewsHandler 点赞新闻
func LikesNewsHandler(c *gin.Context) {
	idStr, ok := c.Params.Get("id")
	id := cast.ToInt(idStr)
	if !ok || id == 0 {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	service.LikesNewsService(c, cast.ToInt(claims.Id), id)
}

// CancelLikesNewsHandler 取消点赞新闻
func CancelLikesNewsHandler(c *gin.Context) {
	idStr, ok := c.Params.Get("id")
	id := cast.ToInt(idStr)
	if !ok || id == 0 {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	service.CancelLikesNewsService(c, cast.ToInt(claims.Id), id)
}

// GetNewsInfoHandler 通过用户id和新闻id获取新闻点赞/收藏信息
func GetNewsInfoHandler(c *gin.Context) {
	idStr, ok := c.Params.Get("id")
	id := cast.ToInt(idStr)
	if !ok || id == 0 {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	service.GetNewsInfoService(c, id)
}
